import {BaseApi} from "./BaseApi.ts";
import {buildNoticeBody, type NoticeBodyRaw} from "../types/Notice.ts";

export class NoticeApi extends BaseApi {
    list = (time_limit?: number | undefined) => this.postNuke({
        __act: "get_all",
        time_limit: time_limit || 0
    }).then(res => {
        if (typeof res.data === "object") return buildNoticeBody(res.data.data)
        const data = res.data.replace(/(\d+):/g, "\"$1\":")
        const body = JSON.parse(data).data[0] as NoticeBodyRaw;
        return buildNoticeBody(body)
    })

    // 清理提醒消息
    clear = () => this.postNuke({__act: "del",}).then(res => res.data)
    // 关闭指定主题或楼层的提醒
    enable = (enable: boolean, tid: number, pid?: number | undefined) => this.postNuke({
        __act: 'set_post_tag',
        no_hint: enable ? 0 : 1,
        tid,
        pid
    }).then(res => res.data.data[0])

    postNuke = (params: any) => this.instance.post("nuke.php", undefined, {
        params: {
            ...params,
            __lib: "noti",
            __output: 8,
        }
    })
}